package com.kinon.gfcanteen_v1.network;

import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.bean.OrderDetailBean;
import com.kinon.gfcanteen_v1.bean.OrdersBean;
import com.kinon.gfcanteen_v1.bean.BalanceBean;
import com.kinon.gfcanteen_v1.bean.PayBean;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by lhqq on 2018-01-09.
 */

public class HttpManage {
    private final  String url="http://gf.zhihuishitang.cn/index.php/";
//    private final  String url="http://test.jishi88.cn/demof/index.php/";
    private static final int TIEE_OUT=10; //超时参数
    private static Retrofit retrofit=null;
    private static RequestApi requestApi=null;
    private static HttpManage httpManage=null;

    public static HttpManage getInstance(){

            if(httpManage==null){
                httpManage=new HttpManage();
            }

        return httpManage;
    }

    public HttpManage() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(ScalarsConverterFactory.create())//增加返回值为string的支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 添加Rx适配器
                .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器
                .client(setHttpClient())
                .build();
        requestApi=retrofit.create(RequestApi.class);
    }

    private OkHttpClient setHttpClient(){
        OkHttpClient.Builder builder=new OkHttpClient.Builder()
                .connectTimeout(TIEE_OUT, TimeUnit.SECONDS)
                .readTimeout(TIEE_OUT,TimeUnit.SECONDS)
                .writeTimeout(TIEE_OUT,TimeUnit.SECONDS)
                .retryOnConnectionFailure(true) //失败重发
                .addInterceptor(new LogInterceptor());
        return builder.build();
    }

    public Call<String> login(String username, String password){
        return requestApi.login(username,password);
    }
    public Call<String> saveNewOrder(String token,String total,int type,String stamp){
        return requestApi.saveNewOrder(token,total,type,stamp);
    }

    public Call<String> otherPay(String token, String orderid, String no, String stamp){
        return requestApi.otherPay(token,orderid,no,stamp);
    }

    public Call<String> recharge(String token,  String no, String money){
        return requestApi.recharge(token,no,money);
    }

    public Call<String> getOrders(String token, int page, int offset){
        return requestApi.getOrders(token,page,offset);
    }

    public Call<String> getDetails(String token, String orderid, String orderno){
        return requestApi.getDetails(token,orderid,orderno);
    }

    public Call<String> updateOrder(String token,int orderid,int state){
        return requestApi.updateOrder(token,orderid,state);
    }

    public Call<String> getUserByCode(String token, String no, int page, int offset){
        return requestApi.getUserByCode(token,no,page,offset);
    }

}
